package mum.edu.cs.waa.assignment.controller;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import mum.edu.cs.waa.assignment.model.Account;
import mum.edu.cs.waa.assignment.model.Database;
import org.omg.CORBA.Context;

/**
 *
 * @author thabtemichael
 */
@WebServlet(urlPatterns = {"/signup"})
public class signup extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String page = "index.jsp";
        String btn = request.getParameter("btn");
        if (!btn.equalsIgnoreCase("Back")) {
            String usrname = request.getParameter("username");
            String password = request.getParameter("password");
            String email = request.getParameter("email");
            String rank = request.getParameter("rank");
            Account acc = new Account(usrname, password, email, Double.parseDouble(rank));
            Database db = (Database) getServletContext().getAttribute("Database");
            db.getAccounts().add(acc);
            getServletContext().setAttribute("Database", db);
            request.getSession().setAttribute("Account", acc);
            page = "signupsuccess.jsp";
        }
        RequestDispatcher tosucess = request.getRequestDispatcher(page);
        tosucess.forward(request, response);
    }

}
